<?php
load_info_var($cf_controller);											//加载smarty变量
load_file('user_class',LIB_PATH);
$thumb_size = array(120,90);

$main = new User($db);
$id = intval($_POST['id']) ? intval($_POST['id']) : intval($_GET['id']);
$uid = intval($_POST['uid']) ? intval($_POST['uid']) : intval($_GET['uid']);
$self = isset($_POST['self']) ? trim($_POST['self']) : trim($_GET['self']);
$userauth = new AdminAuth($db);

switch($cf_action){
	case "index":
		$smarty->mypage($cf_controller,$cf_action,0) ;
		load_file('page_class',LIB_PATH);
		$MyPage = new Mypage($db);		
		$skey = trim($_POST['skey']) ? trim($_POST['skey']) : urldecode(trim($_GET['skey']));		
		$skey2 = trim($_POST['skey2']) ? trim($_POST['skey2']) : urldecode(trim($_GET['skey2']));	
		$skey3 = trim($_POST['skey3']) ? trim($_POST['skey3']) : trim($_GET['skey3']);
		$skey4 = trim($_POST['skey4']) ? trim($_POST['skey4']) : trim($_GET['skey4']);		
		$order = trim($_POST['order']) ? trim($_POST['order']) : trim($_GET['order']);		
		$where = '1';
		if($skey){
			$where .= " AND user_name LIKE '%".$skey."%'";
		}	
		
		if($skey2){
			$where .= " AND user_phone LIKE '%".$skey2."%'";
		}
		
		if($skey3){
        	$where .= " and date_format(user_reg_time,'%Y-%m-%d') = '" . $skey3 . "'";
    	}
		if ($skey4) {
	    	$where .= " and `user_channel_sn` = '" . $skey4 . "'";
		}
		
        if (is_numeric($province)) {

            $where .= " and user_phone_province = '" . $province . "'";
            $city = intval($city);
            if (is_numeric($city)) {
                $where .= " and user_phone_city = '" . $city . "'";
            }
        }
		
		switch($order){
			case "score":
				$key_order = "user_score DESC";
			break;
				
			default:
				$key_order ="user_id DESC";	
			break;
		}
        
		if (is_numeric($self)) {
	    $where .= " and `user_status` = '" . $self . "'";
		}		
		$key_word = '?dpt='.$cf_dpt.'&skey='.$skey. '&self=' . $self.'&skey2='.$skey2.'&skey3='.$skey3.'&skey4='.$skey4. '&province=' . $province. '&city=' . $city;		
		$data_ary = $MyPage->from_sql('*',$main->get_table_name(),$where,$key_order,intval($_GET['cpage']),20);
		$page = $MyPage->get_page($data,$cf_self_page.$key_word.'&order='.$key_order);		
		require_once(INFO_PATH.'cf_var_area.php');
		if($data_ary){			
			foreach($data_ary as $k=>$v){
				$data_ary[$k]['phone_info'] = $cf_var_area[$v['user_phone_province']]['title'].$cf_var_area[$v['user_phone_province']]['child'][$v['user_phone_city']]['title'].$v['user_phone_supplier'];			
			}
		}
		//echo $MyPage->get_sql();
		//dump($data_ary);
	
	break;
	case "add":		
		if($cf_ispost){			
			$user_pwd_new = $_POST['user_pwd_new'];
			$user_pwd_new2 = $_POST['user_pwd_new2'];				
					
			$data = $db->getFormData($main->get_table_name(),$_POST);
			
			if($user_pwd_new || $user_pwd_new2){				
				if(!$user_pwd_new or !$user_pwd_new2)
				my_jump('密码不能为空');
	
				if($user_pwd_new != $user_pwd_new2)
				my_jump('两次输入的密码不一样');
				
				$data['user_pwd'] = base64_encode(md5($user_pwd_new));			
			}			
			//$db->debug(1,1);
			$result = $main->insert_data($data);
			if($result)
			my_jump('添加成功','',$cf_self_page,20);
			else
			my_jump('添加失败');
		
		}else{
			$smarty->mypage($cf_controller,$cf_action,0) ;
		}

    break;
	case "edit":
		$smarty->mypage($cf_controller,$cf_action,0);
		if($cf_ispost){			
			$user_pwd_new = $_POST['user_pwd_new'];
			$user_pwd_new2 = $_POST['user_pwd_new2'];
				
			$data = $db->getFormData($main->get_table_name(),$_POST);
			unset($data['user_pwd']);
			if($user_pwd_new || $user_pwd_new2){				
				if(!$user_pwd_new or !$user_pwd_new2)
				my_jump('密码不能为空');
	
				if($user_pwd_new != $user_pwd_new2)
				my_jump('两次输入的密码不一样');
				
				$data['user_pwd'] = base64_encode(md5($user_pwd_new));
			
			}	
			$data['user_edit_time'] = date("Y-m-d H:i:s");
			//$db->debug(1,1);
			$where = "user_id = '".$id."'";
			$result = $main->update_data($data,$where);
			if($result){
				$return_page = $_SESSION['pre_url'] ? $_SESSION['pre_url'] : $cf_self_page;
				unset($_SESSION['pre_url']);
			my_jump('修改成功','',$return_page,20);
			}else{
			my_jump('修改失败');
			}
		
		}else{				
			require_once(INFO_PATH.'cf_var_area.php');
			$row = $main->get_data_row($id);		
			if($row['user_phone_province'] && $row['user_phone_city'] && $row['user_phone_supplier']){
				$row['phone_info'] = $cf_var_area[$row['user_phone_province']]['title'].$cf_var_area[$row['user_phone_province']]['child'][$row['user_phone_city']]['title'].$row['user_phone_supplier'];	
			}
			$_SESSION['pre_url'] = $cf_pre_page ? $cf_pre_page : '';
		}
		
    break;
	case "del":
		/*if($user_info['g_act_power']!='all')
		my_jump('You do not have permission');
		$row = $user->get_data_row($id);
		$user->del_photo($row['user_photo']);*/
		$result = $user->del_data("id='$id'");
		if($result)
			my_jump('删除成功','',$cf_self_page,2);
		else
			my_jump('删除失败','',$cf_self_page,2);
	break;
	
	case "set":
		$type = $_GET['type'];
		$value = $_GET['value'];
		$where = "user_id = '".$id."'";
		$result = $main->set_data($value,$type,$where);
		if($result)
			my_jump('更新完成','',$cf_self_page,2);
		else 
			my_jump('更新失败');
	break;
	case "status":
		$value = intval($_GET['value']);		
		$reason = trim($_GET['value2']);	
		$where = "user_id = '" . $id . "'";	
		$data = array(
			'user_status' => intval($value),
			'user_close_reason' => $reason,
		);
		//$db->debug(1,1);
		$result = $main->update_data($data, $where);
		$return_page = $cf_module_path.'detail-act_user.htm?id='.$id;
		if ($result) {
			my_jump('操作成功', '', $return_page, 1);
		} else {
			my_jump('操作失败');
		}
	break;
	case "check":
		$value = intval($_GET['value']);		
		$reason = trim($_GET['value2']);	
		$where = "user_id = '" . $id . "'";	
		$data = array(
			'user_check' => intval($value),
			'user_close_reason' => $reason,
		);
		//$db->debug(1,1);
		$result = $main->update_data($data, $where);
		$return_page = $cf_module_path.'detail-act_user.htm?id='.$id;
		if ($result) {
			my_jump('操作成功', '', $return_page, 1);
		} else {
			my_jump('操作失败');
		}
	break;
	case "limit":
		$limit_action = $_GET['value'];		
		$user_info = $main->get_data_row($id,'user_limit_action');		
		if($user_info['user_limit_action']){	
	
			if(strstr($user_info['user_limit_action'],$limit_action)){
				my_jump('这个操作已经限制了');
			}
			$limit_action = $user_info['user_limit_action'].','.$limit_action;
			
		}else{
			$limit_action = $limit_action;
		}
		
		$where = "user_id = '" . $id . "'";
		$data['user_limit_action'] = $limit_action;		
		//$db->debug(1,1);
		$result = $main->update_data($data, $where);
		$return_page = $cf_module_path.'detail-act_user.htm?id='.$id;
		if ($result) {
			my_jump('禁止成功', '', $return_page, 1);
		} else {
			my_jump('禁止失败');
		}
	break;
	case "limitcancel":
		$limit_action = $_GET['value'];
		$user_info = $main->get_data_row($id,'user_limit_action');			
		if($user_info['user_limit_action']){
			$limit_ary = explode(',',$user_info['user_limit_action']);//把字符串转化为数组
			$key = array_search($limit_action,$limit_ary);
			if(is_numeric($key)){
				unset($limit_ary[$key]);
			}			
			$where = "user_id = '" . $id . "'";
			$data['user_limit_action'] = implode(',',$limit_ary);			
			$result = $main->update_data($data, $where);
			$return_page = $cf_module_path.'detail-act_user.htm?id='.$id;
			if ($result) {
				my_jump('操作成功', '', $return_page, 1);
			} else {
				my_jump('操作失败');
			}			
			
			
		}else{
			my_jump('操作失败');
		}
		
		
	break;
	
	case "all":
		$alltype = $_POST['alltype'];
		$idlist = $_POST['idlist'];
		if($idlist){
			$list = implode(",",$idlist);
			switch($alltype){
				
				default:
					my_jump('请指定操作方式');
				break;
			}	
			if($result)
				my_jump('更新完成','',$cf_self_page,2);
			else
				my_jump('更新失败');	
			
		}else{
			my_jump('输入的参数有误');	
		}
	break;
	
	default:
		my_jump('非法操作!');
	break;
}


$smarty->assign(array(
	'id'=>$id,
	'rid'=>$rid,
	'self' => $self,
	'row'=>htmlchar($row),
	'data_ary'=>$data_ary,
	'sort_ary'=>$sort_ary,
	'page'=>$page,
	'skey'=>$skey,
	'skey2'=>$skey2,
	'skey3'=>$skey3,
	'skey4'=>$skey4,
	'skey5'=>$skey5,
	'skey6'=>$skey6,
	'skey7'=>$skey7,
	'skey8'=>$skey8,
	'skey9'=>$skey9,
	'stype'=>$stype,
    'province' => $province,
    'city' => $city,
	'key_word'=>$key_word,
	'type'=>array(''=>'全部','0' => "否", "1" => "是"),
	'fund_search'=>array(''=>'没指定','user_score' => "银票", "user_coin" => "掌币","user_money" => "资金"),
	'limit_ary'=>$limit_ary,
	'row'=>$row,
));
$smarty->mydisplay();


?>